package com.qidian.study.service.impl;

import com.qidian.study.dao.ContentCatDAO;
import com.qidian.study.dao.impl.ContentCatDAOImpl;
import com.qidian.study.pojo.Content;
import com.qidian.study.pojo.ContentCat;
import com.qidian.study.service.ContentCatService;
import com.qidian.study.util.pojo.LayuiTableDTO;
import com.qidian.study.util.pojo.VideoResponse;

import java.util.List;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
public class ContentCatServiceImpl implements ContentCatService {
    private ContentCatDAO catDAO = new ContentCatDAOImpl();
    @Override
    public VideoResponse save(ContentCat cat) {
        if(catDAO.queryByName(cat.getCatName())!=null){
            return VideoResponse.error(505,"名称相同");
        }
        if(catDAO.save(cat)==1){
            return VideoResponse.ok();
        }
        return VideoResponse.error();
    }

    @Override
    public VideoResponse update(ContentCat cat) {
        return catDAO.update(cat)==1?VideoResponse.ok():VideoResponse.error();
    }

    @Override
    public VideoResponse queryById(Long catId) {
        return VideoResponse.ok("OK",catDAO.queryById(catId));
    }

    @Override
    public VideoResponse queryAll() {
        return VideoResponse.ok("OK",catDAO.queryAll());
    }

    @Override
    public LayuiTableDTO queryAllForLayUi() {
        LayuiTableDTO dto = new LayuiTableDTO();
        List<ContentCat> cats = catDAO.queryAll();
        dto.setCode(0);
        dto.setCount(cats.size());
        dto.setData(cats);
        return dto;
    }
}
